/**
 * 修改手册程序（草稿箱修改）
 */
<template>
<div class="layout-parent w100 p10">
    <el-tabs type="border-card">
        <el-tab-pane>
            <template #label><vp-label zh="基本信息" en="Basic Info"/></template>
            <QualityManual ref="manualRef" :loading="loading" :id="id" :type="type"></QualityManual>
        </el-tab-pane>
    </el-tabs>
    <StatusBar
        :id="formData.id"
        :status="formData.zt"
        :isValid="formData.yxbs"
    />
    <PageBottom>
        <vp-btn-save v-if="formData.zt === 10" :loading="saveLoading" @click="handleSave(10)"/>
        <VpProcessToolBar 
            :loading="saveLoading" 
            :before-show-fn="beforeShowFn" 
            :after-agree-fn="afterAgreeFn"
            :set-loading-fn="(_loading: boolean) => { saveLoading = _loading }"
        />    
    </PageBottom>
</div>
</template>
<script setup lang="ts" name="EditManualRevision">
import { ref, reactive, nextTick, onMounted } from 'vue';
import request from '/@/utils/request';
import QualityManual from './QualityManual.vue';
import PageBottom from '/@/components/vp/PageBottom/index.vue';
import { ElMessage } from "element-plus";
import { useRoute } from 'vue-router';
import mittBus from '/@/utils/mitt';
import StatusBar from '/@/components/vp/status/StatusBar.vue';
import VpProcessToolBar from '/@/components/vp/workflow/VpProcessToolBar.vue';

const props = defineProps({
    id: {
		type: String,
        required: true,
	},
});

const loading = ref(false);
const saveLoading = ref(false);
const type = ref('edit');
const formData = ref({
    id: '',
    zt: 10,
    yxbs: 1,
});

const manualRef = ref();
const route = useRoute();
//流程
const processHandleRef = ref();


// 保存
const handleSave = (zt : number) => {
    saveLoading.value = true;
    manualRef.value.getData(zt).then((data: any) =>{
        request.post("/quality/manual/" + type.value, {
            ...data,
            zt: zt,
        }).then(()=>{
            mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
            ElMessage.success(zt == 10 ? "操作成功/Success." : "办理完毕/Completed Processing");
        }).catch(()=>{
        }).finally(()=>{
            saveLoading.value = false;
        })
    }).catch(()=>{saveLoading.value = false;});
}


const beforeShowFn = () =>{
    saveLoading.value = true;
    return manualRef.value.getData(88).finally(()=>{saveLoading.value = false;});
}

const afterAgreeFn = (processHandle:any) =>{
    saveLoading.value = true;
    manualRef.value.getData(20).then((data: any) =>{
        request.post("/quality/manual/" + type.value, {
            ...data,
            zt: 88,
            processHandle: processHandle,
        }).then(()=>{
            mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
            ElMessage.success("操作成功/Operation Succeeded");
        }).catch(()=>{
        }).finally(()=>{
            saveLoading.value = false;
        })
    }).catch(()=>{saveLoading.value = false;});
}

</script>
